#include "kernel_operator.h"
#include <type_traits>
using namespace AscendC;
constexpr int32_t BUFFER_NUM = 2;
template<typename TYPE_Y> class KernelDiv {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelDiv() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, 
                            uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, this->coreDataNum);        
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X1));
        pipe.InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X2));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
        if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>) {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(half));
            pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(half));
        }
        else if constexpr (std::is_same_v<T, int32_t>)
        {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(float));
        }
    }
    __aicore__ inline void Process() {
        int32_t loopCount = this->tileNum;
        this->processDataNum = this->tileDataNum;
        for (int32_t i = 0; i < loopCount; i++) {
            if (i == this->tileNum - 1) {
              this->processDataNum = this->tailDataNum;
            }
            CopyIn(i);
            Compute(i);
            CopyOut(i);
        }
    }

private:
    __aicore__ inline void CopyIn(int32_t progress) {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.AllocTensor<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.AllocTensor<DTYPE_X2>();
        DataCopy(x1Local, x1Gm[progress * this->tileDataNum], this->processDataNum);
        DataCopy(x2Local, x2Gm[progress * this->tileDataNum], this->processDataNum);
        inQueueX1.EnQue(x1Local);
        inQueueX2.EnQue(x2Local);
    }
    __aicore__ inline void Compute(int32_t progress) {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.DeQue<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.DeQue<DTYPE_X2>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();
        if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>) {
            auto tmp1 = QueueTmp1.Get<half>();
            auto tmp2 = QueueTmp2.Get<half>();
            Cast(tmp1, x1Local, RoundMode::CAST_NONE, this->processDataNum);
            Cast(tmp2, x2Local, RoundMode::CAST_NONE, this->processDataNum);

            Div(tmp1, tmp1, tmp2, this->processDataNum);
            Cast(yLocal, tmp1, RoundMode::CAST_TRUNC, this->processDataNum);
        }
        else if constexpr (std::is_same_v<T, int32_t>) {
            
            auto tmp1 = QueueTmp1.Get<float>();
            auto tmp2 = QueueTmp2.Get<float>();
            
            Cast(tmp1, x1Local, RoundMode::CAST_NONE, this->processDataNum);
            Cast(tmp2, x2Local, RoundMode::CAST_NONE, this->processDataNum);

            Div(tmp1, tmp1, tmp2, this->processDataNum);

            Cast(yLocal, tmp1, RoundMode::CAST_TRUNC, this->processDataNum);
            // Duplicate(yLocal,(DTYPE_Y)11,this->processDataNum);
            // Div(yLocal, x1Local, x2Local, this->processDataNum);
            // pipe.InitBuffer(boox, 9999999 * sizeof(float));
            // auto booxxxxx = boox.Get<DTYPE_X1>();
            // Add(booxxxxx, x1Local, x1Local, 9999999);
            // Add(yLocal[1], x1Local[2], x1Local[3], 9999999);
        }
        else {
            // pipe.InitBuffer(boox, 9999999 * sizeof(float));
            // auto booxxxxx = boox.Get<DTYPE_X1>();
            // Add(booxxxxx, x1Local, x1Local, 9999999);
            // Add(yLocal[1], x1Local[2], x1Local[3], 9999999);
            Div(yLocal, x1Local, x2Local, this->processDataNum);
        }
        inQueueX1.FreeTensor(x1Local);
        inQueueX2.FreeTensor(x2Local);
        outQueueY.EnQue<TYPE_Y>(yLocal);
    }
    __aicore__ inline void CopyOut(int32_t progress) {
        LocalTensor<DTYPE_Y> yLocal = outQueueY.DeQue<DTYPE_Y>();
        DataCopy(yGm[progress * this->tileDataNum], yLocal, this->processDataNum);
        outQueueY.FreeTensor(yLocal);
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2,boox;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;
};

template<typename TYPE_Y> class KernelDivBroadcast {
    using T = TYPE_Y;
public:
    __aicore__ inline KernelDivBroadcast() {}
    __aicore__ inline void Init(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, 
                            uint32_t CoreDataNum, uint32_t finalTileNum, uint32_t tileDataNum, uint32_t TailDataNum,
                            int32_t y_dimensional, 
                                int32_t *y_ndarray, int32_t *x1_ndarray, int32_t *x2_ndarray, 
                                int32_t *y_sumndarray, int32_t *x1_sumndarray, int32_t *x2_sumndarray) {
        ASSERT(GetBlockNum() != 0 && "block dim can not be zero!");
        this->coreDataNum = CoreDataNum;
        this->tileNum = finalTileNum;
        this->tileDataNum = tileDataNum;
        this->tailDataNum = TailDataNum;

        this->y_dimensional = y_dimensional;
        for(int k=0; k<=y_dimensional; k++)
        {
            this->y_ndarray[k] = y_ndarray[k];
            this->x1_ndarray[k] = x1_ndarray[k];
            this->x2_ndarray[k] = x2_ndarray[k];
            this->y_sumndarray[k] = y_sumndarray[k];
            this->x1_sumndarray[k] = x1_sumndarray[k];
            this->x2_sumndarray[k] = x2_sumndarray[k];
        }

        x1Gm.SetGlobalBuffer((__gm__ DTYPE_X1*)x1, this->coreDataNum);
        x2Gm.SetGlobalBuffer((__gm__ DTYPE_X2*)x2, this->coreDataNum);        
        yGm.SetGlobalBuffer((__gm__ DTYPE_Y*)y, this->coreDataNum);

        pipe.InitBuffer(inQueueX1, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X1));
        pipe.InitBuffer(inQueueX2, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_X2));
        pipe.InitBuffer(outQueueY, BUFFER_NUM, this->tileDataNum * sizeof(DTYPE_Y));
        if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>) {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(half));
            pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(half));
        }
        else if constexpr (std::is_same_v<T, int32_t>)
        {
            pipe.InitBuffer(QueueTmp1, this->tileDataNum * sizeof(float));
            pipe.InitBuffer(QueueTmp2, this->tileDataNum * sizeof(float));
        }
    }
    __aicore__ inline void Process() {
        LocalTensor<DTYPE_X1> x1Local = inQueueX1.AllocTensor<DTYPE_X1>();
        LocalTensor<DTYPE_X2> x2Local = inQueueX2.AllocTensor<DTYPE_X2>();
        LocalTensor<DTYPE_Y> yLocal = outQueueY.AllocTensor<DTYPE_Y>();
        
        if constexpr (std::is_same_v<T, int8_t> || std::is_same_v<T, signed char>)
        {
            auto tmp1 = QueueTmp1.Get<half>();
            auto tmp2 = QueueTmp2.Get<half>();
            int dim = this->y_dimensional;
            for(int j=0; j<this->y_sumndarray[dim]; j++)
            {
                int x1_start = 0, x2_start = 0;
                for(int k=0; k<dim; k++)
                {
                    if(this->x1_ndarray[k] != 1){
                        x1_start += this->x1_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }
                    if(this->x2_ndarray[k] != 1){
                        x2_start += this->x2_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }

                }
                DTYPE_X1 x1 = x1Gm.GetValue(x1_start);
                DTYPE_X2 x2 = x2Gm.GetValue(x2_start);
                x1Local.SetValue(0, (DTYPE_Y)x1);
                x2Local.SetValue(0, (DTYPE_Y)x2);
                Cast(tmp1, x1Local, RoundMode::CAST_NONE, 1);
                Cast(tmp2, x2Local, RoundMode::CAST_NONE, 1);

                Div(tmp2, tmp1, tmp2, 1);
                Cast(tmp1.ReinterpretCast<int16_t>(), tmp2, RoundMode::CAST_RINT, 1);
                ShiftLeft(tmp1.ReinterpretCast<int16_t>(), tmp1.ReinterpretCast<int16_t>(), int16_t(8), 1);
                ShiftRight(tmp1.ReinterpretCast<int16_t>(), tmp1.ReinterpretCast<int16_t>(), int16_t(8), 1);

                Cast(tmp2, tmp1.ReinterpretCast<int16_t>(), RoundMode::CAST_NONE, 1);
                Cast(yLocal, tmp2, RoundMode::CAST_NONE, 1);
                yGm.SetValue(j, (DTYPE_Y)yLocal.GetValue(0));
            }
        }
        else if constexpr (std::is_same_v<T, int32_t>) {

            auto tmp1 = QueueTmp1.Get<float>();
            auto tmp2 = QueueTmp2.Get<float>();
            int dim = this->y_dimensional;
            for(int j=0; j<this->y_sumndarray[dim]; j++)
            {
                int x1_start = 0, x2_start = 0;
                for(int k=0; k<dim; k++)
                {
                    if(this->x1_ndarray[k] != 1){
                        x1_start += this->x1_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }
                    if(this->x2_ndarray[k] != 1){
                        x2_start += this->x2_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }

                }
                DTYPE_X1 x1 = x1Gm.GetValue(x1_start);
                DTYPE_X2 x2 = x2Gm.GetValue(x2_start);
                x1Local.SetValue(0, (DTYPE_Y)x1);
                x2Local.SetValue(0, (DTYPE_Y)x2);
                Cast(tmp1, x1Local, RoundMode::CAST_NONE, 1);
                Cast(tmp2, x2Local, RoundMode::CAST_NONE, 1);
                Div(tmp2, tmp1, tmp2, 1);
                Cast(yLocal, tmp2, RoundMode::CAST_NONE, 1);
                yGm.SetValue(j, (DTYPE_Y)yLocal.GetValue(0));
            }
        }
        else
        {
            int dim = this->y_dimensional;
            for(int j=0; j<this->y_sumndarray[dim]; j++)
            {
                int x1_start = 0, x2_start = 0;
                for(int k=0; k<dim; k++)
                {
                    if(this->x1_ndarray[k] != 1){
                        x1_start += this->x1_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }
                    if(this->x2_ndarray[k] != 1){
                        x2_start += this->x2_sumndarray[k] * (j / this->y_sumndarray[k] % this->y_ndarray[k]);
                    }

                }
                DTYPE_X1 x1 = x1Gm.GetValue(x1_start);
                DTYPE_X2 x2 = x2Gm.GetValue(x2_start);
                x1Local.SetValue(0, (DTYPE_Y)x1);
                x2Local.SetValue(0, (DTYPE_Y)x2);
                Div(yLocal, x1Local, x2Local, 1);
                yGm.SetValue(j, yLocal.GetValue(0));
            }
            // for(int j=0; j<=dim; j++)
            //     yGm.SetValue(j, (DTYPE_Y)this->y_ndarray[j]);
            // for(int j=0; j<=dim; j++)
            //     yGm.SetValue(j+dim, (DTYPE_Y)this->x1_sumndarray[j]);
            // for(int j=0; j<=dim; j++)
            //     yGm.SetValue(j+dim*2, (DTYPE_Y)this->x2_sumndarray[j]);
                // yGm.SetValue(j, (DTYPE_Y)1);
        }
    }

private:
    TPipe pipe;
    TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX1, inQueueX2;
    TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueY;
    TBuf<QuePosition::VECCALC> QueueTmp1, QueueTmp2;

    GlobalTensor<DTYPE_X1> x1Gm;
    GlobalTensor<DTYPE_X2> x2Gm;
    GlobalTensor<DTYPE_Y> yGm;
    uint32_t coreDataNum;
    uint32_t tileNum;
    uint32_t tileDataNum;
    uint32_t tailDataNum;
    uint32_t processDataNum;

    int32_t y_dimensional;
    int32_t y_ndarray[20];
    int32_t x1_ndarray[20];
    int32_t x2_ndarray[20];

    int32_t y_sumndarray[20];
    int32_t x1_sumndarray[20];
    int32_t x2_sumndarray[20];
};
extern "C" __global__ __aicore__ void div(GM_ADDR x1, GM_ADDR x2, GM_ADDR y, GM_ADDR workspace, GM_ADDR tiling) {
    GET_TILING_DATA(tiling_data, tiling);
    // TODO: user kernel impl
    if (TILING_KEY_IS(1)) {
        KernelDiv<DTYPE_Y> op;
        op.Init(x1, x2, y, 
                tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum);
        op.Process();
    }
    else if (TILING_KEY_IS(2)) {
        KernelDivBroadcast<DTYPE_Y> op;
        op.Init(x1, x2, y, 
                tiling_data.CoreDataNum, tiling_data.finalTileNum, tiling_data.tileDataNum, tiling_data.TailDataNum,
                tiling_data.y_dimensional,
                tiling_data.y_ndarray, tiling_data.x1_ndarray, tiling_data.x2_ndarray,
                tiling_data.y_sumndarray, tiling_data.x1_sumndarray, tiling_data.x2_sumndarray);
        op.Process();
    }
}